import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_common/util/context_extension.dart';

import '../../flutter_common.dart';

/// 左文本右输入框的方式
class TextInputItem extends StatelessWidget {
  final String name;
  final String hint;

  ///背景主题颜色
  final Color? backgroundColor;

  ///name字体颜色
  final Color? color;

  ///value字体颜色
  final Color? inputColor;

  ///hint字体颜色
  final Color? hintColor;
  final double? fontSize;
  final TextEditingController? controller;
  final ValueChanged<String>? onChanged;

  ///禁止输入
  final bool disabled;
  final double height;
  final double? textFieldHeight;
  final bool showLine;
  final bool showRed;
  ///[showLine]开启的时候，设置线左边的边距
  final double lineLeftPadding;
  final int? maxLength;
  final int? maxLines;
  final TextInputType? keyboardType;
  final TextInputAction? textInputAction;
  final List<TextInputFormatter>? input;
  final TextAlign textAlign;
  final VoidCallback? onEditingComplete;
  final ValueChanged<String>? onSubmitted;
  final Widget? nameWidget;
  final Widget? suffixWidget;
  final CrossAxisAlignment crossAxisAlignment;
  final EdgeInsetsGeometry? contentPadding;
  final EdgeInsetsGeometry? namePadding;
  final FocusNode? focusNode;
  final TextStyle? style;
  final TextStyle? hintStyle;

  const TextInputItem({
    Key? key,
    this.name = "",
    this.hint = "请输入",
    this.fontSize,
    this.backgroundColor,
    this.color = const Color(0xff333333),
    this.inputColor = const Color(0xff111111),
    this.hintColor = const Color(0xffBBBBBB),
    this.controller,
    this.onChanged,
    this.showLine = false,
    this.showRed = false,
    this.nameWidget,
    this.lineLeftPadding = 0,
    this.height = 50,
    this.textFieldHeight,
    this.disabled = false,
    this.maxLength,
    this.maxLines = 1,
    this.keyboardType,
    this.input,
    this.suffixWidget,
    this.textAlign = TextAlign.right,
    this.crossAxisAlignment = CrossAxisAlignment.center,
    this.contentPadding = const EdgeInsets.fromLTRB(0, 12, 0, 12),
    this.namePadding = const EdgeInsets.fromLTRB(0, 12, 0, 12),
    this.textInputAction = TextInputAction.done,
    this.onEditingComplete,
    this.onSubmitted,
    this.focusNode,
    this.style,
    this.hintStyle,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // if (disabled) {
    //   //如果禁止输入，则显示左右文本
    //   return LeftRightTextItem(
    //       showLine: showLine,
    //       lineLeftPadding: lineLeftPadding,
    //       title: name,
    //       content: controller == null ? '' : controller!.text,
    //       titleStyle: TextStyle(fontSize: fontSize, color: color),
    //       contentStyle: TextStyle(fontSize: fontSize, color: inputColor));
    // } else {
      return _enableWidget(context);
    // }
  }

  Widget _enableWidget(BuildContext context) {
    Widget body = Row(
      crossAxisAlignment: crossAxisAlignment,
      children: [
        SizedBox(width: 15,height: height),
        Container(
          constraints: BoxConstraints(minHeight: height),
          padding: namePadding,
          alignment: Alignment.center,
          child: nameWidget ??
              Text.rich(TextSpan(
                children: [
                  if (showRed)
                    const TextSpan(
                        text: "*", style: TextStyle(color: Colors.red)),
                  TextSpan(text: name),
                ],
                style: TextStyle(fontSize: fontSize ?? fontSize28, color: color),
              )),
        ),
        const SizedBox(width: 8),
        Expanded(child: ConstrainedBox(
          constraints: BoxConstraints(
            minHeight: textFieldHeight ?? 0,
          ),
          child: _edit(),
        )),
        if(suffixWidget != null) suffixWidget!,
        const SizedBox(width: 15),
      ],
    );
    if (showLine) {
      body = Stack(children: [
        body,
        Positioned(
          child: FxTheme().line,
          bottom: 0,
          left: lineLeftPadding,
          right: 0,
        )
      ]);
    }
    return Container(color: backgroundColor, child: ConstrainedBox(
      constraints: BoxConstraints(
        minHeight: height,
      ),
      child: body,
    ));
  }

  Widget _edit() {
    List<TextInputFormatter>? _input;
    if(keyboardType == TextInputType.phone || keyboardType == TextInputType.number) {
      //因安卓的输入法会输入字母等其他字符，故通过代码的方式限制输入
      _input = [FilteringTextInputFormatter.allow(RegExp(r'[0123456789+-]'))];
    } else {
      _input = input;
    }
    return TextField(
      enabled: !disabled,
      controller: controller,
      keyboardType: keyboardType,
      textAlign: textAlign,
      maxLength: maxLength,
      inputFormatters: _input,
      focusNode: focusNode,
      textInputAction: textInputAction,
      decoration: InputDecoration(
        border: InputBorder.none,
        disabledBorder: InputBorder.none,
        enabledBorder: InputBorder.none,
        counterText: "",
        // contentPadding: EdgeInsets.fromLTRB(0, padding.top, padding.right, padding.bottom),
        contentPadding: contentPadding,
        hintText: hint,
        hintStyle: TextStyle(
          fontSize: fontSize ?? fontSize28,
          color: hintColor,
        ).merge(hintStyle),
      ),
      maxLines: maxLines,
      style: TextStyle(
        fontSize: fontSize ?? fontSize28,
        color: inputColor,
      ).merge(style),
      onChanged: onChanged,
      onEditingComplete: onEditingComplete,
      onSubmitted: onSubmitted,
    );
  }
}
